// pages/checkin/checkin.js

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      schedules:[],
      record:{},
      task:{},
      taskInfo:'',
      showInfo:false,
      showSuccess:false,
      showRecord:false,
      checkRecord:{},
      taskRecord:{},
      showRooms:false,
      rooms:[],
      roomName:"",
      room:{},
      showButton:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const deviceId=app.globalData.room&&app.globalData.room.HID;
    const username=app.globalData.localuserinfo.username;
    this.setData({roomName:app.globalData.room&&app.globalData.room.name||"开放教室"})
    const that=this;
    wx.request({
      url: `https://${app.globalData.checkin_url}/door/getTodayLastRecord?deviceId=${deviceId}&user_number=${username}`,
      method: 'get',
      header: {
        'content-type': 'application/json'
      },
      success: res2 => {
        const data=res2.data;
        console.log('getTodayLastRecord',data);
        if(data&&data.task==='showCheckin'){
        wx.request({
            url: `https://${app.globalData.checkin_url}/door/checkByUserNumber?deviceId=${deviceId}&user_number=${username}`,
            method: 'get',
            header: {
              'content-type': 'application/json'
            },
            success: res3 => {
              const data=res3.data;
              console.log(data);
              if(data.task==='showInfo'){
                this.setData({showInfo:true,taskInfo:data.info})
              }else if(data.task==='showSuccess'){
                this.setData({showSuccess:true,taskInfo:data.success,taskRecord:data.record,roomName:data.record&&data.record.roomName})
              }else if(data.task==='showRooms'){
                this.setData({showRooms:true,rooms:data.rooms})
              }

            }
          })
        }
        else if (data&&data.task==='checkout'){
            this.setData({showRecord:true,checkRecord:data.record})
        }
      }
    })
    
  },

  checkin(e){
    const room=e.target.id;
    const rooms=this.data.rooms;
    const scedule=rooms.find(r=>r.room===room);
    const deviceId=app.globalData.room.HID;
    const that=this;
      wx.request({
        url: `https://${app.globalData.checkin_url}/door/checkinRoom?deviceId=${deviceId}`,
        data: {...scedule},
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: res2 => {
          const data=res2.data;
          console.log('data',data)
          this.setData({
            showRooms:false,
            showSuccess:true,
            taskInfo:data.success,
            taskRecord:data.record,
            roomName:data.record&&data.record.roomName||'',
          })
        }
      })
  },

  checkout(){
    const deviceId=app.globalData.room.HID;
    const username=app.globalData.localuserinfo.username;
    const that=this;
      wx.request({
        url: `https://${app.globalData.checkin_url}/door/checkout?deviceId=${deviceId}&username=${username}`,
        method: 'get',
        header: {
          'content-type': 'application/json'
        },
        success: res2 => {
          const data=res2.data;
          this.setData({
            showRecord:false,
            showSuccess:true,
            taskInfo:'签出成功',
            taskRecord:that.data.checkRecord,
            roomName:data.record&&data.record.roomName||'',
          })
        }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})